package com.zggao.annotation;



import java.lang.annotation.Annotation;
import java.lang.reflect.Field;


public class ParseAnnotation {
    // 解析类上的注解
    public static void parseTypeAnnotation() throws ClassNotFoundException {
        Class aClass = Class.forName("com.zggao.annotation.PersonInfoAnnotation");
        Annotation[] annotations = aClass.getAnnotations();
        for(Annotation a:annotations){
            CourseAnnotation ca = (CourseAnnotation)a;
            System.out.println(ca.name()+","+ca.age());
        }
    }

    // 解析成员变量上的注解
    public static void parseFieldAnnotation() throws ClassNotFoundException {
        Class aClass = Class.forName("com.zggao.annotation.MyCourse");
        Field[] fields = aClass.getDeclaredFields();
        for (Field f:fields){
            // 判断成员变量中是否有指定的类型的注解
            boolean hasAnnotation = f.isAnnotationPresent(PersonInfoAnnotation.class);
            if(hasAnnotation){
                PersonInfoAnnotation ca = f.getAnnotation(PersonInfoAnnotation.class);
                System.out.println(ca.name() + ca.age());
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        parseFieldAnnotation();
    }
}
